package lumis.portal.presentation.perspective.admin;

import lumis.portal.presentation.tag.page.RenderHolderComponent;

import org.apache.wicket.MarkupContainer;
import org.apache.wicket.Component.IVisitor;
import org.apache.wicket.ajax.AjaxRequestTarget;

/**
 * Behavior that provides the javascript for processing a move of an interface,
 * during the edit of a page.
 *
 * @version $Revision: 13093 $ $Date: 2011-05-28 18:40:18 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
@SuppressWarnings("serial")
public class MoveInterfaceBehavior extends DropInterfaceBehavior
{
	@Override
	protected void respond(final AjaxRequestTarget target)
	{
		// remove srcII
		MarkupContainer container = (MarkupContainer) getComponent();
		final String srcII = container.getRequest().getParameter("srcII");
		container.visitChildren(RenderHolderComponent.class, new IVisitor<RenderHolderComponent>()
		{
			public Object component(RenderHolderComponent component)
			{
				if (component.removeServiceInterfaceInstance(srcII))
				{
					target.addComponent(component);
					return STOP_TRAVERSAL;
				}
				else
				{
					return CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER;
				}
			}
		});

		// insert srcII at new position
		insertInterfaceInstance(srcII, target);
	}
	
	@Override
	protected String getFunctionName()
	{
		return "moveInterface";
	}
}